﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RedDragon.Common.Domain.Purchase;
using RedDragon.Common.Dtos;
using RedDragon.ERP.API.Application.Commands.Purchases;
using System.Globalization;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Controllers
{
    /// <summary>
    /// 采购/委外订单
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class PurchaseController : ControllerBase
    {
        private readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public PurchaseController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 获取所属项目信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchAffiliatedProject")]
        public async Task<Result> SearchAffiliatedProject([FromQuery] SearchAffiliatedProjectCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取订单类型信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchPurchaseType")]
        public async Task<Result> SearchPurchaseType([FromQuery] SearchPurchaseTypeCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取所属项目信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchAllSupplier")]
        public async Task<Result> SearchAllSupplier([FromQuery] SearchAllSupplierCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取币种信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchCurrency")]
        public async Task<Result> SearchCurrency([FromQuery] SearchCurrencyCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 添加采购/委外订单信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("CreatePurchaseOutsource")]
        public async Task<Result> CreatePurchaseOutsource(CreatePurchaseOutsourceCommand cmd)
        {
            cmd.CreatedTime = DateTime.Now;
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取采购/委外订单信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("SearchPurchaseOutsource")]
        public async Task<Result<PageResultDto<PurchaseOutsourcesDto>>> SearchPurchaseOutsource(SearchPurchaseOutsourceCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取单位信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("SearchUint")]
        public async Task<Result> SearchUint([FromQuery] SearchUnitCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 添加订单行信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("CreateOrderBank")]
        public async Task<Result> CreateOrderBank(CreateOrderBankCommand cmd)
        {
            cmd.CreatedTime = DateTime.Now;
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取订单行信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("SearchOrderBank")]
        public async Task<Result<PageResultDto<OrderBankDto>>> SearchOrderBank(SearchOrderBankCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 添加付款计划行信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("CreatePayment")]
        public async Task<Result> CreatePayment(CreatePaymentCommand cmd)
        {
            cmd.CreatedTime = DateTime.Now;
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 获取付款计划行信息
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("SearchPayment")]
        public async Task<Result<PageResultDto<Payments>>> SearchPayment(SearchPaymentCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 采购/委外订单反填
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPost("DetailPurchase")]
        public async Task<Result<PurchaseOutsources>> DetailPurchase(DetailPurchaseCommand cmd)
        {
            return await _mediator.Send(cmd);
        }

        /// <summary>
        /// 修改采购/委外订单
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpPut("UpdatePurchase")]
        public async Task<Result> UpdatePurchase(UpdatePurchaseCommand cmd)
        {
            cmd.UpdateTime = DateTime.Now;
            return await _mediator.Send(cmd);
        }


        /// <summary>
        /// 导出采购/委外订单
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        [HttpGet("ExportPurchase")]
        public async Task<IActionResult> ExportPurchase([FromQuery] ExportPurchaseCommand cmd)
        {
            var res = await _mediator.Send(cmd);
            return File(res.FileBytes, res.MimeType, res.FileName);
        }
    }
}
